package com.hdit.net
{
	import com.hdit.mgr.LogMgr;
	
	import mx.messaging.channels.HTTPChannel;
	import mx.messaging.messages.HTTPRequestMessage;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;
    
	public class XMLLoader
	{
		public static const OBJECT:String="object"; 
		public static const ARRAY:String="array";
		public static const XML:String="xml";
		public static const FLASHVARS:String="flashvars";
		public static const TEXT:String="text";
		public static const E4X:String="e4x";
		
		public function XMLLoader()
		{
		}
		
		/**
		 * 加载XML 可转对象树
		 * @param url 要加载XML链接地址
		 * @param 
		 **/
		
		public static function load(url:String,resultType:String,callback:Function):void
		{
			var result:Object=null;
			var http:HTTPService=new HTTPService();
			var callBackFun:Function=callback;
			http.url=url;
			if(resultType==OBJECT || resultType==XML || resultType== ARRAY || resultType== TEXT || resultType==E4X || resultType==FLASHVARS)
            {
                http.resultFormat=resultType; 
            }
            else
            {
                trace("resultType类型错误!");
            }
			http.addEventListener(ResultEvent.RESULT,onResult,false,0,true);
			http.addEventListener(FaultEvent.FAULT,onFault,false,0,true);
			http.send();
			function onResult(e:ResultEvent):void
			{
				callBackFun.call(null,e.result as Object);
			}
			function onFault(e:FaultEvent):void
			{
				callBackFun.call(null,null);
				LogMgr.error(e.fault.toString());
			}
		}
	}
}